package com.platform.config;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.Filter;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.platform.common.session.JedisSessionDAO;
import com.platform.modules.sys.oauth2.Oauth2Filter;
import com.platform.modules.sys.oauth2.Oauth2Realm;

/**
 * Shiro配置
 *
 * @author 肖亮
 */
@Configuration
public class ShiroConfig {

	@Bean
	public JedisSessionDAO getJedisSessionDAO() {
		return new JedisSessionDAO();
	}

	@Bean("sessionManager")
	public SessionManager sessionManager() {
		DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
		sessionManager.setSessionValidationSchedulerEnabled(true);
		sessionManager.setSessionIdCookieEnabled(true);
		sessionManager.setSessionDAO(getJedisSessionDAO());
		return sessionManager;
	}

	@Bean("securityManager")
	public SecurityManager securityManager(Oauth2Realm oAuth2Realm, SessionManager sessionManager) {
		DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
		securityManager.setRealm(oAuth2Realm);
		securityManager.setSessionManager(sessionManager);

		return securityManager;
	}

	@Bean("shiroFilter")
	public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
		ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
		shiroFilter.setSecurityManager(securityManager);

		// oauth过滤
		Map<String, Filter> filters = new HashMap<>(16);
		filters.put("oauth2", new Oauth2Filter());
		shiroFilter.setFilters(filters);

		Map<String, String> filterMap = new LinkedHashMap<>();
		filterMap.put("/druid/**", "anon");
		filterMap.put("/app/**", "anon");
		filterMap.put("/sys/login", "anon");
		filterMap.put("/captcha.jpg", "anon");
		filterMap.put("/sys/dict/**", "anon");
		filterMap.put("/reg/registerAptitudeUser", "anon");
		filterMap.put("/reg/saveOrg", "anon");   
		filterMap.put("/reg/displayOrgResult", "anon");  
//		filterMap.put("/reg/verification", "anon");
		filterMap.put("/reg/verification", "anon");
//		filterMap.put("/reg/**", "anon");
		filterMap.put("/doc.html", "anon");
		filterMap.put("/v2/api-docs", "anon");
		filterMap.put("/v2/api-docs-ext", "anon");
		filterMap.put("/swagger-ui/**", "anon");
		filterMap.put("/swagger-resources/**", "anon");
		filterMap.put("/diagram-viewer/**", "anon");
		filterMap.put("/editor-app/**", "anon");
		filterMap.put("/modeler.html", "anon");
		//filterMap.put("/act/**", "anon");
		//filterMap.put("/process/**", "anon");
		//filterMap.put("/km/**", "anon");
		filterMap.put("/service/**", "anon");
		filterMap.put("/statics/**", "anon");
		filterMap.put("/login.html", "anon");
		filterMap.put("/templates/**", "anon");
		filterMap.put("/main.html", "anon");
		filterMap.put("/index.html", "anon");
		filterMap.put("/sys/loginByApp", "anon");
		filterMap.put("/sys/logoutByApp", "anon");
		filterMap.put("/prj/declare/attachment", "anon");
		filterMap.put("/prj/declare/attachmentCertifiPDF", "anon");
		filterMap.put("/prj/declare/attachmentReportPDF", "anon");
		filterMap.put("/prj/declare/attachmentPolicyReportPDF", "anon");
		filterMap.put("/prj/declare/saveSeal", "anon");
		filterMap.put("/prj/declare/hgsSaveSeal", "anon");
		filterMap.put("/report/**", "anon");
		filterMap.put("/sys/org/getOrgInfomation", "anon");
		filterMap.put("/reg/sendEmailBoxAgain", "anon");
		filterMap.put("/webservice/weChatUserWebService", "anon");
		filterMap.put("/webservice/**", "anon");
		filterMap.put("/prj/declare/download1", "anon");
		filterMap.put("/prj/declare/download", "anon");
		filterMap.put("/**", "oauth2");

		shiroFilter.setFilterChainDefinitionMap(filterMap);

		return shiroFilter;
	}

	@Bean("lifecycleBeanPostProcessor")
	public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
		return new LifecycleBeanPostProcessor();
	}

	@Bean
	public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
		DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
		proxyCreator.setProxyTargetClass(true);
		return proxyCreator;
	}

	@Bean
	public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
		AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
		advisor.setSecurityManager(securityManager);
		return advisor;
	}

}
